import { Component, OnInit } from '@angular/core';
import { MrpManageService } from '../mrp-manage.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { HCommonService } from 'src/app/h-common/h-common.service';

@Component({
    selector: 'app-mrp-list',
    templateUrl: './mrp-list.component.html',
    styleUrls: ['./mrp-list.component.scss']
})

export class MrpListComponent implements OnInit {

    pageIndex: number = 1;
    pageSize: number = 10;
    listData = [];
    mrpData = [];
    selectMrpData = [];
    listCount: number = 0;
    isShow: boolean = false;
    listForm: FormGroup;
    selectItem: { id?: number } = {};
    isOkLoading: boolean = false;
    isDelete: boolean = false;
    bornRole = {}

    constructor(
        private mrpService: MrpManageService,
        private fb: FormBuilder,
        private commonService : HCommonService
    ) { }

    ngOnInit() {
        /* 获取权限 */
        this.bornRole = this.commonService.getRoles("erpWarehouse")

        this.listForm = this.fb.group({
            warehouseId: [null, Validators.required]
        })
        this.getErpList();
        this.getMrpList();
    }
    getErpList() {
        let _dataJson = {
            id: "",
            page: this.pageIndex,
            size: this.pageSize
        }
        this.mrpService.apiErpList(_dataJson).subscribe(res => {
            this.listData = res.content;
            this.listCount = res.totalElements;
        })
    }
    getMrpList() {
        let _dataJson = {}
        this.mrpService.apiMrpList(_dataJson).subscribe(res => {
            this.mrpData = res;
        })
    }
    getArea(data) {
        this.mrpService.detailData = data;
    }
    showModal(data) {
        this.initModal(data);
        this.selectMrpData = this.mrpData;
        this.isDelete = false;
    }
    handleCancel() {
        this.isShow = false;
    }
    submitForm() {
        for (const i in this.listForm.controls) {
            this.listForm.controls[i].markAsDirty();
            this.listForm.controls[i].updateValueAndValidity();
        }
        if (this.listForm.status === "VALID") {
            this.isOkLoading = true;
            let _dataJson = {
                erpWarehouseId: this.selectItem.id,
                warehouseId: this.listForm.controls["warehouseId"].value
            }
            if (this.isDelete) {
                console.log( _dataJson );
                this.mrpService.apiHouseDelete(_dataJson).subscribe(res => {
                    this.initStatus();
                }, () => {
                    this.isOkLoading = false;
                })
            } else {
                this.mrpService.apiHouseSave(_dataJson).subscribe(res => {
                    this.initStatus();
                }, () => {
                    this.isOkLoading = false;
                })
            }
        }
    }
    delete(data) {
        this.initModal(data);
        this.selectMrpData = data.warehouses;
        this.isDelete = true;
    }
    initStatus() {
        this.isShow = false;
        this.isOkLoading = false;
        this.getErpList();
    }
    initModal(data){
        this.isShow = true;
        this.listForm.reset();
        this.selectItem = data;
    }
}
